Skip to content

sed: treat an empty last line without a newline as a line on output - #692

Open
Xnadir wants to merge 2 commits into
uutils:mainfrom
Xnadir:fix/empty-unterminated-line
Open

Xnadir wants to merge 2 commits into
uutils:mainfrom
Xnadir:fix/empty-unterminated-line

Conversation

@Xnadir

@Xnadir Xnadir commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

When the last line lacks its newline and the pattern space is empty, printing it should still separate the output into lines, as GNU sed does:

$ printf 'ab' | sed 's/ab//p'
                    # GNU: one empty line ("\n"); uutils: nothing
$ printf 'x' | sed 'p;s/x//'
x                   # GNU: "x\n"; uutils: "x"

OutputBuffer::write_chunk returned early for an empty chunk without a newline, so the line neither ended the previous output's line nor made the next output start a new one. The early return was also how a deleted cycle (d, c, D) avoided the auto-print, since those leave a cleared pattern space of the same shape. So c, d and D now set a deleted flag that skips the auto-print, and write_chunk treats an empty unterminated chunk as a line: it writes any pending newline and leaves its own pending.

The new test covers the cases above, plus empty output that must stay empty (s/x// on x, and $!d;s/x//). It fails on main. The other integration tests are unchanged. On my Windows checkout, the same 48 fail on main and on this branch. I also re-ran a few hundred scripts comparing against GNU sed 4.9 with no new differences.

AI disclosure: written with help from Claude Code.

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings November 17, 2026 15:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.12%. Comparing base (1a27699) to head (90f2a2e).

Files with missing lines Patch % Lines
src/sed/fast_io.rs 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #692      +/-   ##
==========================================
- Coverage   94.14%   94.12%   -0.03%     
==========================================
  Files          14       14              
  Lines        5741     5749       +8     
  Branches      458      458              
==========================================
+ Hits         5405     5411       +6     
- Misses        335      337       +2     
  Partials        1        1              
Flag Coverage Δ
macos_latest 96.12% <100.00%> (+<0.01%) ⬆️
ubuntu_latest 96.00% <100.00%> (+<0.01%) ⬆️
windows_latest 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 3.45%

1 warning

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 1 improved benchmark
✅ 10 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ access_log_all_del 856.2 ms 827.7 ms +3.45%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Xnadir:fix/empty-unterminated-line (90f2a2e) with main (1a27699)

Open in CodSpeed

Comment thread tests/by-util/test_sed.rs
("p;s/x//", "x", "x\n"),
("s/x//;p", "a\nx", "a\na\n\n"),
("s/x//", "x", ""),
("$!d;s/x//", "a\nx", ""),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add cases for q and c/D too? e.g. s/x//;q and $!N;D on an empty unterminated last line, since they go through the new path as well

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Xnadir
Xnadir force-pushed the fix/empty-unterminated-line branch from ef09359 to 90f2a2e Compare November 26, 2026 01:29
Copilot AI balanced review requested due to automatic review settings November 26, 2026 01:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants